//
//  VCOfCellForExploreSectionOne.m
//  echo
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "VCOfCellForExploreSectionOne.h"
#import "CellSectionZeroForVCOfCellForExploreSectionOne.h"
#import "CellSectionOneForVCOfCellForExploreSectionOne.h"
#import "NetworkingHandler.h"
#import "ModelForExploreTableViewCell.h"
#import "ModelOfCellSectionZeroForVCOfCellForExploreSectionOne.h"
#import "ModelForRecommendTableViewCell.h"
#import "MusicPlayerVC.h"
#import <DKNightVersion.h>

@interface VCOfCellForExploreSectionOne ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UILabel *labelForTitle;
@property (nonatomic, retain)NSMutableArray *arrForSectionZero;
@property (nonatomic, retain)NSMutableArray *arrForSectionOne;
@property (nonatomic, retain)NSMutableArray *tempArr;
@end

@implementation VCOfCellForExploreSectionOne

- (void)dealloc{
    self.tableView.delegate = nil;
    [_tableView release];
    [_arrForSectionZero release];
    [_arrForSectionOne release];
    [_tempArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    [self handleDataForSectionZero];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForLeftOfExploreSectionOne" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForRecommendTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelForRecommendTableViewCell:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    }];

    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForRightOfExploreSectionOne" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForRecommendTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelForRecommendTableViewCell:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    }];
    
}

- (void)handleDataForSectionZero {
    NSString *str1 = @"http://echosystem.kibey.com/channel/info?limit=20&id=";
    NSString *str2 = [str1 stringByAppendingFormat:@"%@", self.id];
    NSString *url = [str2 stringByAppendingString:@"&list_order=hot&page=1"];
    [NetworkingHandler networkingGetWithUrl:url dicForValue:@{@"x-v":@"20160209", @"x-c":@"3", @"Cookie":@"PHPSESSID=av1vbh1jkjocs4r4evmrk6j095"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSDictionary *dic2 = [dic objectForKey:@"data"];
        NSDictionary *dic3 = [dic2 objectForKey:@"channel"];
        self.arrForSectionZero = [NSMutableArray array];
        self.arrForSectionOne = [NSMutableArray array];
        self.tempArr = [NSMutableArray array];
        ModelOfCellSectionZeroForVCOfCellForExploreSectionOne *model = [[ModelOfCellSectionZeroForVCOfCellForExploreSectionOne alloc]init];
        [model setValuesForKeysWithDictionary:dic3];
        [self.arrForSectionZero addObject:model];
        [model release];
        NSArray *arr = [dic2 objectForKey:@"sounds"];
        for (NSDictionary *tempDic in arr) {
            ModelForRecommendTableViewCell *modelForSectionOne = [[ModelForRecommendTableViewCell alloc]init];
            [modelForSectionOne setValuesForKeysWithDictionary:tempDic];
            NSDictionary *tempDic2 = [tempDic objectForKey:@"user"];
            modelForSectionOne.channel = [tempDic2 objectForKey:@"name"];
            [self.tempArr addObject:modelForSectionOne];
            [modelForSectionOne release];
        }

        for (int i = 0; i < self.tempArr.count; i++) {
            if (i%2 == 0) {
                NSMutableArray *tempArr2 = [NSMutableArray array];
                [tempArr2 addObject:self.tempArr[i]];
                [tempArr2 addObject:self.tempArr[i+1]];
                [self.arrForSectionOne addObject:tempArr2];
            }
        }
        
        
        [self createTableView];
    }];
}

- (void)createTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.frame = self.view.frame;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[CellSectionZeroForVCOfCellForExploreSectionOne class] forCellReuseIdentifier:@"poorForCellSectionZero"];
    [self.tableView registerClass:[CellSectionOneForVCOfCellForExploreSectionOne class] forCellReuseIdentifier:@"poorForCellSectionOne"];
    
    self.labelForTitle = [[UILabel alloc]initWithFrame:CGRectMake(50 * WIDTH, 10 * HEIGHT, self.view.frame.size.width - 100 * WIDTH, 40 * HEIGHT)];
    self.labelForTitle.textColor = [UIColor whiteColor];
    self.labelForTitle.font = [UIFont fontWithName:@"Arial-BoldMT" size:16];
    ModelOfCellSectionZeroForVCOfCellForExploreSectionOne *model = [self.arrForSectionZero objectAtIndex:0];
    self.labelForTitle.text = model.name;
    [self.view addSubview:self.labelForTitle];
    self.labelForTitle.textAlignment = 1;
    UIButton *buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonForBack setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    buttonForBack.frame = CGRectMake(7 * WIDTH, 15 * HEIGHT, 40 * WIDTH, 35 * HEIGHT);
    [self.view addSubview:buttonForBack];
    [buttonForBack addTarget:self action:@selector(handleBackForVCOfCellForExplore) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleBackForVCOfCellForExplore{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ModelOfCellSectionZeroForVCOfCellForExploreSectionOne *model = [self.arrForSectionZero objectAtIndex:indexPath.row];
        CellSectionZeroForVCOfCellForExploreSectionOne *cell = [tableView dequeueReusableCellWithIdentifier:@"poorForCellSectionZero"];
        [cell passModel:model];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else{
        CellSectionOneForVCOfCellForExploreSectionOne *cell = [tableView dequeueReusableCellWithIdentifier:@"poorForCellSectionOne"];
        NSArray *arr = [self.arrForSectionOne objectAtIndex:indexPath.row];
        [cell passModelForButtons:arr];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 335 * HEIGHT;
        
    }else{
        return 257 * HEIGHT;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01 * HEIGHT;
    }else{
        return 20 * HEIGHT;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01 * HEIGHT;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
        view.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        return view;
    }else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
        view.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        return view;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else{
        
        return self.arrForSectionOne.count;
    }
}

// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
